package com.rock.commonbo.impl;

import java.util.HashMap;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.rock.common.annotation.ANNBaseService;
import com.rock.commonbo.api.ICommonBoService;
import com.rock.commonbo.mapper.CommonBoMapper;

/**
 * 类名称：
 */
@ANNBaseService
@Repository
@Service
public class CommonBoService implements ICommonBoService {

	private static Logger _log = LoggerFactory.getLogger(CommonBoService.class);
	@Autowired
	CommonBoMapper commonBoMapper;

	@Override
	public void initMapper() {
		_log.trace("===>commonBo inited");
	}

	@Override
	public int getMaxId(String tablename, String key) {
		return commonBoMapper.findMaxId(tablename, key);
	}

	@Override
	public List<HashMap<String, Object>> query(String sql) {
		return commonBoMapper.executeSelect(sql);
	}

	@Override
	public void runSQL(String sql) {
		if (sql != "" && sql != null) {
			String cmd = StringUtils.trimLeadingWhitespace(sql).toLowerCase().substring(0, 6);
			_log.trace("===>cmd：" + cmd);
			if (cmd.equals("update")) {
				_log.trace("===>执行更新：" + sql);
				commonBoMapper.executeUpdate(sql);
			} else if (cmd.equals("delete")) {
				_log.trace("===>执行删除：" + sql);
				commonBoMapper.executeDelete(sql);
			} else if (cmd.equals("insert")) {
				_log.info("===>runSQL暂不支持插入操作");
			} else if (cmd.equals("select")) {
				_log.info("===>runSQL暂不支持查询操作");
			} else {
				_log.error("===>无此关键字：update，delete，insert，select");
			}
		} else {
			_log.error("===>sql 语句为空");
		}
	}
}